﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gamego
{

    /// <summary>
    /// 游戏类
    /// </summary>
    class Game
    {
        //游戏长宽
        private const int GameWidth = 700;
        private const int GameHeight = 450;


        //定义障碍物的数组
        private List<Barrier> barriers { get; set; }
        //定义委托
        public delegate void GameChanged();
        //定义事件
        public event GameChanged gamechanged;
        private DateTime NextbBrrierTime { get; set; }
        public static DateTime GameStartTime { get; set; }
        private Random rnd { get; set; }

        /// <summary>
        /// 游戏自带的Timer
        /// </summary>
        private System.Timers.Timer Timer { get; set; }

        //定义障碍物的数组
        private Point[] barrierPonits =
            {
           new Point(100,50),
            new Point(200,50),
            new Point(300,50),
            new Point(400,50),
             };

        private Bar _bar;

        public Game()
        {
            //初始化开始时间
            NextbBrrierTime = System.DateTime.Now.AddMilliseconds(1);
            _bar = new Bar(GameWidth);
            //初始化障碍物
            barriers = new List<Barrier>();
            //处理障碍物落下


            //初始化时间
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();

        }
        /// <summary>
        /// 处理碰撞函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        // 判断游戏结束条件
        public bool GameOver()
        {
            foreach (var Barrier in barriers)
            {

                if (_bar.Rectangle.IntersectsWith(Barrier.Rectangle))
                {
                    Timer.Stop();
                    return true;
                }
              
            }
            return false;
        }

            //时钟处理程序

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            //处理碰撞

            foreach (var Barrier in barriers)
            {
                Barrier.Move();
                //处理碰撞
               
                
            }
            
            if(System.DateTime.Now >NextbBrrierTime)
            {

                //定义随机数
                if (rnd == null)
                    rnd = new Random();
                int newX = 70 + rnd.Next(0, 5) * 100;
                //随机产生相同的障碍物
                Barrier barrier = new Barrier(new Rectangle(newX, 0, 150, 30));
                barriers.Add(barrier);
                
                //随机时间产生下个障碍物
                int newTime = rnd.Next(1000, 1500);
                NextbBrrierTime = System.DateTime.Now.AddMilliseconds(newTime);
            }

            

            
            //处理时钟非空

            if(gamechanged!=null)
            {
                gamechanged();
            }
        }

        /// <summary>
        /// 绘制当前
        /// </summary>
        /// <param name="g">绘图句柄</param>
        /// <param name="size">游戏区域尺寸</param>
        public void Draw(Graphics g, Size size)

        {
            //空白的图
            Image img = new Bitmap(GameWidth, GameHeight);

            Graphics _g = Graphics.FromImage(img);

            //定义一个矩形
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));

            _g.DrawImage(Properties.Resources.tea , rec);


            _bar.Draw(_g);
            foreach (var barrier in barriers)
            {
                barrier.Draw(_g);
            }

            //todo

            g.DrawImage(img, new Rectangle(new Point(0, 0), size));
        }/// <summary>
         /// 处理键盘
         /// </summary>
        public void KeyDown(String key)
        {
            _bar.KeyDown(key);


        }
    }
}